﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraLayout;
using DiaoPaiDaYin;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.UC.门店下单.service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.门店下单.下单
{
    public class RepositoryItemSearchLookUpEdit可选设计点 :RepositoryItemSearchLookUpEdit
    {
        #region repositoryItemSearchLookUpEdit1View;
        private DevExpress.XtraGrid.Views.Grid.GridView repositoryItemSearchLookUpEdit1View = new DevExpress.XtraGrid.Views.Grid.GridView();
        #endregion
        public DTODesignPoints Dto设计点;

        public RepositoryItemSearchLookUpEdit可选设计点(DesignPointsFacadeDTO designPointsFacadeDTO) {
            //  属性
            this.bandAttribute();
            //  event
            this.bandEvent();
            //  绑定数据
            this.Dto设计点 = designPointsFacadeDTO.defaultDtoDesignPoints;
            this.DataSource = designPointsFacadeDTO.optionDtoDesignPoints;
        }
        /// <summary>
        /// 设置属性
        /// </summary>
        private void bandAttribute()
        {
            this.addColumns();  //  下拉view的column
            this.AutoHeight = false;
            this.Buttons.AddRange(
                new DevExpress.XtraEditors.Controls.EditorButton[] {
                    new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)}
                );
            //this.Name = "repositoryItemSearchLookUpEdit" + dr["ITEM_VALUE"].ToString() ;
            this.PopupView = this.repositoryItemSearchLookUpEdit1View;
        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        private void bandEvent()
        {
            this.Popup += new System.EventHandler(this.searchLookUpEdit设计点_Popup);
            this.repositoryItemSearchLookUpEdit1View.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(this.searchLookUpEdit设计点View_RowCellClick);
            this.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(this.searchLookUpEdit设计点_CustomDisplayText);
        }

        /// <summary>
        /// 下拉view的column
        /// </summary>
        private void addColumns()
        {
            //id
            DevExpress.XtraGrid.Columns.GridColumn colID = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colID });
            colID.Caption = "itemTypeNameCn";
            colID.FieldName = "itemTypeNameCn";
            colID.Name = "itemTypeNameCn";
            colID.Visible = false;
            colID.VisibleIndex = -1;
            //ITEM_CD
            DevExpress.XtraGrid.Columns.GridColumn colITEM_CD = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colITEM_CD });
            colITEM_CD.Caption = "ITEM_CD";
            colITEM_CD.FieldName = "itemCd";
            colITEM_CD.Name = "colITEM_CD";
            colITEM_CD.Visible = false;
            colITEM_CD.VisibleIndex = -1;
            //ITEM_VALUE
            DevExpress.XtraGrid.Columns.GridColumn colITEM_VALUE = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colITEM_VALUE });
            colITEM_VALUE.Caption = "ITEM_VALUE";
            colITEM_VALUE.FieldName = "itemValue";
            colITEM_VALUE.Name = "colITEM_VALUE";
            colITEM_VALUE.Visible = false;
            colITEM_VALUE.VisibleIndex = -1;
            //OPTION_VALUE
            DevExpress.XtraGrid.Columns.GridColumn colOPTION_VALUE = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colOPTION_VALUE });
            //colOPTION_VALUE.Caption = "OPTION_VALUE";
            colOPTION_VALUE.Caption = "编码";
            colOPTION_VALUE.FieldName = "optionValue";
            colOPTION_VALUE.Name = "colOPTION_VALUE";
            colOPTION_VALUE.Visible = true;
            colOPTION_VALUE.VisibleIndex = 0;
            //ITEM_NAME_CN
            DevExpress.XtraGrid.Columns.GridColumn colITEM_NAME_CN = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemSearchLookUpEdit1View.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colITEM_NAME_CN });
            colITEM_NAME_CN.Caption = "名称";
            colITEM_NAME_CN.FieldName = "name";
            colITEM_NAME_CN.Name = "colITEM_NAME_CN";
            colITEM_NAME_CN.Visible = true;
            colITEM_NAME_CN.VisibleIndex = 1;
        }

        #region 选择设计点
        private void searchLookUpEdit设计点View_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            this.Dto设计点.itemTypeNameCn = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "itemTypeNameCn");
            this.Dto设计点.itemCd = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "itemCd");
            this.Dto设计点.itemValue = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "itemValue");
            this.Dto设计点.optionValue = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "optionValue");
            this.Dto设计点.name = FunctionHelper.GetgridViewValue((GridView)sender, e.RowHandle, "name");
        }

        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit设计点_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                ////得到取消按钮，重写点击事件
                //Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                //LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                //clearBtn.Click += clearBtn_Click;

                ////添加一个simplebutton控件(确认按钮)
                //LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                //myLCI.TextVisible = false;
            }
        }
        
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit设计点_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            //((SearchLookUpEdit)sender).Properties.View.GetRowCellValue(rowHandle, "ITEM_VALUE").ToString()
            if (null != e.Value)
                //e.DisplayText = this.OPTION_VALUE + ":" + this.ITEM_NAME_CN;
            e.DisplayText = this.Dto设计点.optionValue + ":" + this.Dto设计点.name;
        }

        private void searchLookUpEdit设计点_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "colOPTION_VALUE")
            {
                if (this.Dto设计点.optionValue != this.Dto设计点.defaultValue)
                {
                    e.Appearance.BackColor = Color.Red;
                }
            }
        }
        #endregion
    }
}
